package cn.wolfcode.rbac.controller;

import cn.wolfcode.rbac.domain.Department;
import cn.wolfcode.rbac.domain.Permission;
import cn.wolfcode.rbac.domain.Role;
import cn.wolfcode.rbac.query.PageResult;
import cn.wolfcode.rbac.query.QueryObject;
import cn.wolfcode.rbac.service.IPermissionService;
import cn.wolfcode.rbac.service.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/role")
public class roleController {

    @Autowired
    private IRoleService roleService;
    @Autowired
    private IPermissionService permissionService;

    @RequestMapping("/list")
    public String listAll(Model model, QueryObject qo){
        PageResult roles = roleService.query(qo);
        model.addAttribute("pageResult",roles);
        return "role/list";
    }

    @RequestMapping("/delete")
    public String delete(Long id){
        roleService.delete(id);
        return "redirect:/role/list";
    }

    @RequestMapping("/input")
    public String input(Model model,Long id){
        if(id!=null){
            Role one = roleService.getOne(id);
            model.addAttribute("role",one);
        }

        List<Permission> permissions=permissionService.listAll();
        model.addAttribute("permissions",permissions);

        return "role/input";
    }

    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Role role,Long[] permissionIds){
        roleService.saveOrUpdate(role,permissionIds);
        return "redirect:/role/list";
    }
}
